package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseConvert;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService courseBaseService;

    /*
    分页查询课程数据
     */
    @PostMapping("/course/list")
    public PageVO queryCourseListByPage(PageRequestParams pagePrarms, @RequestBody QueryCourseModel model) {
        //使用工具获取请求头中的令牌，来校验访问这进行机构隔离
        Long companyId = SecurityUtil.getCompanyId();

        //service方法参数中添加令牌参数
        PageVO pageVO = courseBaseService.queryCurseListByPage(pagePrarms, model, companyId);
        return pageVO;
    }

    /*
    添加课程
     */
    @PostMapping("course")
    public CourseBaseDTO insertCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        Long companyId = SecurityUtil.getCompanyId();

        CourseBaseDTO baseDTO = CourseConvert.INSTANCE.vo2DTO(courseBaseVO);
        baseDTO.setCompanyId(companyId);
        CourseBaseDTO courseBaseDTO = courseBaseService.insertCourseBase(baseDTO);
        return courseBaseDTO;
    }

    /*
    根据id查询课程信息
     */
    @GetMapping("/course/{courseBaseId}")
    public CourseBaseDTO queryByCourseID(@PathVariable Long courseBaseId) {

        //获取课程id
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO courseBaseDTO = courseBaseService.queryByCourseID(courseBaseId, companyId);
        return courseBaseDTO;
    }

    /*
    修改课程信息
     */
    @PutMapping("/course")
    public CourseBaseDTO updateCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //获取机构id
        Long companyId = SecurityUtil.getCompanyId();

        CourseBaseDTO baseDTO = CourseConvert.INSTANCE.vo2DTO(courseBaseVO);
        baseDTO.setCompanyId(companyId);

        CourseBaseDTO courseBaseDTO = courseBaseService.updateCourseBase(baseDTO);
        return courseBaseDTO;
    }

    /*
    删除课程信息
     */
    @DeleteMapping("/course/{courseId}")
    public Boolean deleteCourseBase(@PathVariable Long courseId) {
        Long companyId = SecurityUtil.getCompanyId();
        return courseBaseService.deleteCourseBase(courseId, companyId);

    }

    /*
     课程提交
     */
    @GetMapping("/course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        //获取机构id
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.commitCourseBase(courseBaseId,companyId);
    }
}
